import pojo.Goods;

import java.sql.*;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;

import static java.lang.Math.pow;

public class pointCount {
    public static void main(String[] args) throws Exception {

        String url = "jdbc:mysql://localhost:3306/gdut_shop";
        String username = "root";
        String password = "123456";
        Connection connection = DriverManager.getConnection(url, username, password);

        PreparedStatement preparedStatement = connection.prepareStatement("select goods_id, create_time, point, ask, looks from goods");
        ResultSet resultSet = preparedStatement.executeQuery();
        Goods goods = null;

        //读取数据
        if (resultSet.next()){
            goods = new Goods();
            int goodsId = resultSet.getInt("goods_id");
            Timestamp createTime = resultSet.getTimestamp("create_time");
            double point = resultSet.getDouble("point");
            int ask = resultSet.getInt("ask");
            int looks = resultSet.getInt("looks");

            //计算时间差
            Timestamp currentTime = Timestamp.from(Instant.now());
            LocalDateTime start = createTime.toLocalDateTime();
            LocalDateTime end = currentTime.toLocalDateTime();
            Duration duration = Duration.between(start, end);
            long days = duration.toDays();

            //为对象的属性赋值
            goods.setGoodsId(goodsId);
            goods.setGoodsAsks(ask);
            goods.setCreatTime(createTime);
            goods.setGoodsLooks(looks);
            goods.setGoodsPoints(looks + ask * 5 + pow(0.85, days) * point);
        }

        //System.out.println(goods);

        resultSet.close();
        preparedStatement.close();
        connection.close();
    }

}
